package com.slipper.ai.model.embedding.support;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;
import com.slipper.ai.model.embedding.EmbeddingModelBuilder;
import com.slipper.core.gson.GsonUtil;
import com.slipper.exception.CommonException;
import com.slipper.system.ai.bo.AiModelBO;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;

import java.util.Map;

public class DashScopeEmbeddingModelBuilder implements EmbeddingModelBuilder {
    @Override
    public EmbeddingModel build(AiModelBO model) {
        Map<String, Object> options = GsonUtil.toMap(model.getParamJson());
        if (options == null || options.isEmpty() || !options.containsKey("apiUrl")) {
            throw new CommonException("参数配置不正确！");
        }

        String apiUrl = (String) options.get("apiUrl");
        String apiKey = (String) options.get("apiKey");
        return new DashScopeEmbeddingModel(
                DashScopeApi.builder().baseUrl(apiUrl).apiKey(apiKey).build(),
                MetadataMode.EMBED,
                DashScopeEmbeddingOptions.builder().withModel(model.getName()).build());
    }
}
